<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *设备维护记录
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'记录列表'),
	array('op'=>'edit','title'=>'添加记录'),	
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$data['title like']='%'.trim($_RQ['title']).'%';
		$data['uniacid']=$_SESSION['uniacid'];
		$did=pdo_getcolumn('jy_smallquan_device',$data,'id');
		$params['did']=$did;
		$return['params']['did']=$did;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','did','managerid','content','status','repairtime');
	$list=pdo_getslice('jy_smallquan_device_repair',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$device=pdo_get('jy_smallquan_device',array('id'=>$item['did']));
			$manager=pdo_get('jy_smallquan_manager',array('id'=>$item['managerid']));
			$item['device']=$device;
			$item['manager']=$manager;
			$item['repairtime']=date('y-m-d h:i',$item['repairtime']);

		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'device.title','title'=>'设备名称','type'=>'string'),
			array('name'=>'manager.name','title'=>'管家名称','type'=>'string'),
			array('name'=>'manager.avatar','title'=>'管家头像','type'=>'image'),
			array('name'=>'content','title'=>'维护详情','type'=>'string'),
			array('name'=>'repairtime','title'=>'维护时间','type'=>'string'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'设备名称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_smallquan_device_repair',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'did','title'=>'选择设备','type'=>'select','options'=>'device'),
					array('name'=>'managerid','title'=>'选择管家','type'=>'select','options'=>'manager','option_title'=>'name'),
					array('name'=>'content','title'=>'维护详情','type'=>'text'),
					array('name'=>'repairtime','title'=>'时间','type'=>'time'),
				)
			),
		)
	);
	$return['device']=pdo_getall('jy_smallquan_device_classify',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['manager']=pdo_getall('jy_smallquan_manager',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['htmls']=$htmls;
	//print_r($return['lev_name']);exit;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	//exi($postdata['lev_name'],'error');exit;
	$data=array(
		'content'=>trim($postdata['content']),	
		'did'=>intval($postdata['did']),	
		'managerid'=>intval($postdata['managerid']),		
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_smallquan_device_repair',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['repairtime']=TIMESTAMP;
		pdo_insert('jy_smallquan_device_repair',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_smallquan_device_repair',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}